#include "../include/head.h"
#include "../include/utils.h"

// 进入人才管理系统
void enter_system()
{
    while (1)
    {
        int option = 0;
        system("clear");
        printf("---------------------欢迎来到人才管理系统------------------\n");
        printf("请做出你的选择\n");
        printf("1、注册\n");
        printf("2、登录\n");
        printf("3、退出\n");
        printf("请做出你的选择(1~3):");
        scanf("%d", &option);
        switch (option)
        {
        case 1:
            /* 注册 */
            logon();
            break;
        case 2:
            /*登录*/
            log_in_menu();
            break;
        case 3:
            /*退出*/
            return;
            break;
        default:
            break;
        }
    }
}

// 登录系统
void log_in_menu()
{
    int op = 0;
    system("clear");
    printf("---------------------欢迎来到登录界面------------------\n");

    printf(" 1、管理员登录\n");
    printf(" 2、老师登录\n");
    printf(" 3、学生登录\n");
    printf(" 4、退出\n");
    printf("请做出你的选择(1~4):");
    scanf("%d", &op);
    switch (op)
    {
    case 1:
        Admin_Func(alist); // 进入管理员登录系统
        break;
    case 2:
        Teacher_Func(tlist); // 进入老师登录系统
        break;
    case 3:
        Student_Func(slist); // 进入学生登录系统
        break;
    case 4:
        return;
        break;
    default:
        break;
    }
}

// 学生登录
void Student_Func(p_list list)
{
    if (list == NULL)
    {
        printf("student list is error\n");
        return;
    }
    p_list user_id = log_in(list);
    if (user_id == NULL)
    {
        printf("登录失败\n");
        sleep(2);
        return;
    }
    system("clear");
    printf("------------------欢迎来到学生登录界面-------------------\n");
    int option = 0;
    while (1)
    {
        printf("你可以进行如下操作\n");
        printf("1、查看个人信息\n");
        printf("2、查看各科成绩\n");
        printf("3、选课\n");
        printf("4、修改密码\n");
        printf("5、退出\n");
        printf("请输入您的选择:");
        scanf("%d", &option);
        switch (option)
        {
        case 1:
        {
            /* 查看个人信息 */
            system("clear");
            printf("姓名\t\t    学号\t\t性别\t\t班级\t\t年龄\t\t密码\n");
            printf("%s\t\t%s\t\t%s\t\t%s\t\t%d\t\t%s\t\n", user_id->data.name, user_id->data.id,
                   user_id->data.sex, user_id->data.class,
                   user_id->data.age, user_id->data.password);
            break;
        }
        case 2:
        {
            /* 查看各科成绩 */
            printf("%s\t", user_id->data.name);
            for (size_t i = 0; i < initcount; i++)
            {
                if(strcmp(user_id->data.course[i].coursename,"NULL"))
                {
                    printf("%s: %d\t", user_id->data.course[i].coursename, user_id->data.course[i].score);
                }
            }
            printf("\n");
            break;
        }
        case 3:
        {
            /* 选课 */
            // 记录选课前的课程数量(initcount会变)
            int flag = initcount;
            choose_course(user_id);
            p_list p = slist->next;
            if (flag == initcount)
            {
                while (p != NULL)
                {
                    if (p == user_id)
                    {
                        p = p->next;
                    }
                    strcpy(p->data.course[initcount].coursename, "NULL");
                    p->data.course[initcount].score = 0;
                    p = p->next;
                }
            }
            initcount++;
            printf("已选课程:");
            for (size_t i = 0; i < initcount; i++)
            {
                if (strcmp(user_id->data.course[i].coursename, "NULL") != 0)
                {
                    printf("%s\t", user_id->data.course[i].coursename);
                }
            }
            printf("\n");
            list_update_file(slist, STUDENTS_FILE);
            break;
        }
        case 4:
        {
            /* 修改密码 */
            change_password(slist, user_id->data.id);
            system("clear");
            show_list(slist);
            list_update_file(slist, STUDENTS_FILE);
            break;
        }
        case 5:
            /* 退出 */
            return;
            break;
        default:
            break;
        }
    }
    return;
}

// 管理员登录
void Admin_Func(p_list list)
{
    if (list == NULL)
    {
        printf("admin list is error\n");
        return;
    }
    if ((log_in(list)) == NULL)
    {
        printf("登录失败\n");
        sleep(2);
        return;
    }
    system("clear");
    printf("------------------欢迎来到管理员登录界面-------------------\n");
    int option = 0;
    while (1)
    {
        printf("你可以进行如下操作\n");
        printf("1、增加老师账户\n");
        printf("2、删除老师账户\n");
        printf("3、增加学生账号\n");
        printf("4、删除学生账户\n");
        printf("5、查询老师信息\n");
        printf("6、修改老师密码\n");
        printf("7、查看选课情况\n");
        printf("8、退出\n");
        printf("请做出你的选择:");
        scanf("%d", &option);
        switch (option)
        {
        case 1:
        {
            /* 增加老师账户 */
            user_data data;
            printf("请输入老师的姓名:");
            scanf("%s", data.name);
            printf("请输入老师的工号:");
            scanf("%s", data.id);
            printf("请输入老师的性别:");
            scanf("%s", data.sex);
            printf("请输入老师所授课程:");
            scanf("%s", data.class);
            printf("请输入老师的年龄:");
            scanf("%d", &data.age);
            printf("请输入老师的密码:");
            scanf("%s", data.password);

            printf("注册成功,请登录\n");
            insert_list(tlist, data);
            system("clear");
            show_list(tlist);
            list_update_file(tlist, TEACHER_FILE);
            break;
        }
        case 2:
        {
            /* 删除老师账户 */
            del_list(tlist);
            system("clear");
            show_list(tlist);
            list_update_file(tlist, TEACHER_FILE);
            break;
        }
        case 3:
        {
            /* 增加学生信息 */
            user_data data;
            printf("请输入你要添加的信息\n");
            printf("姓名:");
            scanf("%s", data.name);
            printf("学号:");
            scanf("%s", data.id);
            printf("性别:");
            scanf("%s", data.sex);
            printf("班级:");
            scanf("%s", data.class);
            printf("年龄:");
            scanf("%d", &data.age);
            printf("密码:");
            scanf("%s", data.password);
            char class[2][20] = {"数学分析", "数据结构"};
            for (size_t i = 0; i < 2; i++)
            {
                strcpy(data.course[i].coursename, class[i]);
                printf("%s成绩:", data.course[i].coursename);
                data.course[i].score=0;
            }
            for (size_t i = 2; i < initcount; i++)
            {
                strcpy(data.course[i].coursename, "NULL");
                data.course[i].score = 0;
            }
            insert_list(slist, data);
            system("clear");
            show_list(slist);
            list_update_file(slist, STUDENTS_FILE);
            break;
        }
        case 4:
        { /* 删除学生信息 */
            del_list(slist);
            system("clear");
            show_list(slist);
            list_update_file(slist, STUDENTS_FILE);
            break;
        }
        case 5:
        {
            /* 查询老师信息 */
            p_list phead;
            char id[20];
            printf("请输入您要查询的账号:");
            scanf("%s", id);
            phead = find_list(tlist, id);
            system("clear");
            if (phead != NULL)
            {
                printf("姓名\t工号\t性别\t所授课程\t年龄\t密码\n");
                printf("%s\t%s\t%s\t%s\t%d\t%s\n", phead->data.name, phead->data.id,
                       phead->data.sex, phead->data.class, phead->data.age, phead->data.password);
            }
            else
            {
                printf("没有这个老师\n");
            }
            break;
        }
        case 6:
        {
            /* 修改老师密码 */
            char id[20];
            printf("请输入你要修改的账户:");
            scanf("%s", id);
            change_password(tlist, id);
            system("clear");
            show_list(tlist);
            list_update_file(tlist, TEACHER_FILE);
            break;
        }
        case 7:
        {
            /* 查看选课情况 */
            course_file(slist);
            break;
        }
        case 8:
        {
            /*退出*/
            return;
            break;
        }
        default:
            break;
        }
    }
    return;
}

// 老师登录
void Teacher_Func(p_list list)
{
    if (list == NULL)
    {
        printf("teacher list is error\n");
        return;
    }
    p_list p = log_in(list);
    if (p == NULL)
    {
        printf("登录失败\n");
        sleep(2);
        return;
    }
    system("clear");
    printf("------------------欢迎来到老师登录界面-------------------\n");
    int option = 0;
    while (1)
    {
        printf("你可以进行如下操作\n");
        printf("1、查询学生信息\n");
        printf("2、修改学生信息\n");
        printf("3、将学生成绩排序\n");
        printf("4、显示所有学生信息\n");
        printf("5、录入学生成绩\n");
        printf("6、查看课程被选情况\n");
        printf("7、退出\n");
        printf("请做出你的选择:");
        scanf("%d", &option);
        switch (option)
        {
        case 1:
        {
            /* 查询学生信息 */
            system("clear");
            p_list phead;
            char id[20];
            printf("请输入您要查询的账号:");
            scanf("%s", id);
            phead = find_list(slist, id);
            if (phead != NULL)
            {
                printf("%s\t%s\t%s\t%s\t%d\t%s", phead->data.name, phead->data.id,
                       phead->data.sex, phead->data.class,
                       phead->data.age, phead->data.password);
                for (size_t i = 0; i < initcount; i++)
                {
                    if(strcmp(phead->data.course[i].coursename,"NULL")!=0)
                    {
                        printf("\t%s\t%d", phead->data.course[i].coursename, phead->data.course[i].score);
                    } 
                }
                printf("\n");
            }
            else
            {
                printf("没有这个学生\n");
            }
            break;
        }
        case 2:
        {
            /* 修改学生信息 */
            system("clear");
            teacher_change_list(slist);
            show_list(slist);
            list_update_file(slist, STUDENTS_FILE);
            break;
        }
        case 3:
        {
            /* 将学生按成绩排序 */
            system("clear");
            sort_class(slist);
            sort_scorce(slist);
            break;
        }
        case 4:
        {
            /* 显示所有学生信息 */
            //按学号排序
            system("clear");
            sort_class(slist);
            sort_id(slist);
            list_update_file(slist, STUDENTS_FILE);
            break;
        }
        case 5:
        {
            /* 录入学生成绩 */  
            print_scorse(slist, p);
            sleep(1);
            p_list phead = slist->next;
            system("clear");
            printf("该课程所有学生成绩\n");
            while (phead != NULL)
            {
                for (size_t i = 0; i < initcount; i++)
                {
                    if (strcmp(p->data.class, phead->data.course[i].coursename) == 0)
                    {
                        printf("%s\t%s的\t%s成绩为:%d\n", phead->data.id, phead->data.name, phead->data.course[i].coursename,phead->data.course[i].score);
                    }
                }
                phead = phead->next;
            }
            break;
        }
        case 6:
        {
            /* 查看课程被选情况 */
            system("clear");
            student_choose(p);
            break;
        }
        case 7:
        {
            /*退出*/
            return;
            break;
        }
        default:
            break;
        }
    }
    return;
}